package com.qfedu.openplateform.manager.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.entity.AppInfo;
import com.qfedu.openplateform.manager.entity.Res;
import com.qfedu.openplateform.manager.service.AppInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sys/app_info")
public class AppInfoController {

    @Autowired
    private AppInfoService appInfoService;

    @RequestMapping("/table")
    public Res findByPage(
            @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
            @RequestParam(value = "PageSize",required = false,defaultValue = "10")Integer pageSize
    ){
        PageHelper.startPage(pageNum,pageSize);
        List<AppInfo> userList = appInfoService.getAppInfoList();
        PageInfo<AppInfo> pageInfo = new PageInfo<AppInfo>(userList);
        return Res.ok(pageInfo.getList(),pageInfo.getTotal());
    }



    @RequestMapping("/add")
    public Res addAppInfo(AppInfo appInfo){
        try {
            appInfoService.addAppInfo(appInfo);
            return Res.ok(true, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Res.error(false, "添加失败");
    }




    @RequestMapping("/del")
    public Res del(Integer[] ids){
        try {
            appInfoService.del(ids);
            return Res.ok(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Res.error(false, "删除失败");
    }



    @RequestMapping("/update")
    public Res update(AppInfo appInfo){
        try {
            appInfoService.update(appInfo);
            return Res.ok(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Res.error(false,"修改失败");
    }
}
